﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 舵机调试工具
{
    public partial class SoverControl : UserControl
    {
        #region 初始化代码

        public int index = 0;


        private int _thisAngle = 0;
        public int thisAngle
        {
            get
            {
                return _thisAngle;
            }
            set
            {
                NUD_Angle.Value = value;
                _thisAngle = value;
            }
        }
        public SoverControl()
        {
            InitializeComponent();

            //NUD_Angle数据和TrB_Angle的数据进行绑定
            //运行效果是，两个控件随便更改一个，那么另一个控件也会跟着变
            //代码语法如下：
            //A.DataBindings.Add("a", B, "b"); A.a的数据随着B.b的数据改变
            //下边两句话表示相互绑定
            NUD_Angle.DataBindings.Add("Value", TrB_Angle, "Value");
            TrB_Angle.DataBindings.Add("Value", NUD_Angle, "Value");

            IsLock = true;
            MinAngle = 0;
            MaxAngle = 180;
        }
        #endregion

        #region 关键属性
        private bool _isLock = true;
        /// <summary>
        /// 控件锁定标志
        /// </summary>
        public bool IsLock
        {
            get
            {
                return _isLock;
            }
            set
            {
                _isLock = value;
                if (_isLock)
                {
                    TB_ID.Enabled = false;
                    TB_Function.Enabled = false;
                    NUD_Min.Visible = false;
                    NUD_Max.Visible = false;
                    LB_Min.Visible = false;
                    LB_Max.Visible = false;
                    LAB_Del.Visible = false;
                    LAB_MoveP.Visible = false;
                    LAB_MoveN.Visible = false;
                }
                else
                {
                    TB_ID.Enabled = true;
                    TB_Function.Enabled = true;
                    NUD_Min.Visible = true;
                    NUD_Max.Visible = true;
                    LB_Min.Visible = true;
                    LB_Max.Visible = true;
                    LAB_Del.Visible=true;
                    LAB_MoveP.Visible = true;
                    LAB_MoveN.Visible = true;
                }
            }
        }

        private bool _isDelete = false;
        public bool IsDelete
        {
            //只有get没有set，只读
            get
            {
                return _isDelete;
            }
        }

        private bool _isMoveP = false;
        public bool IsMoveP
        {
            get
            {

                bool v = _isMoveP;
                if(v)
                _isMoveP = false ;
                return v;
            }
        }

        private bool _isMoveN = false;
        public bool IsMoveN
        {
            get
            {
                bool v = _isMoveN;
                if (v)
                    _isMoveN = false;
                return v;
            }
        }

        private int _id = 0;
        public int ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id=value;
                TB_ID.Text =value.ToString();
            }
        }

        private string _function = "功能";
        public string Function
        {
            get
            {
                return _function;
            }
            set
            {
                TB_Function.Text = value.ToString();
                _function = value;
            }
        }

        private int _minAngle = 0;
        public int MinAngle
        {
            get
            {
                return _minAngle;
            }
            set
            {
                _minAngle = value;
                TrB_Angle.Minimum = value;
                NUD_Angle.Minimum = value;
                NUD_Angle.Value = value;
            }
        }

        private int _maxAngle = 0;
        public int MaxAngle
        {
            get
            {
                return _maxAngle;
            }
            set
            {
                _maxAngle = value;
                TrB_Angle.Maximum = value;
                NUD_Angle.Maximum = value;
            }
        }
        #endregion

        private void NUD_ValueChanged(object sender, EventArgs e)
        {
            if (sender == NUD_Angle)
            {
                thisAngle = (int)NUD_Angle.Value;
                angleChanged?.Invoke(index, thisAngle);
            }
            else if (sender == NUD_Min)
            {
                MinAngle = (int)NUD_Min.Value;
            }
            else if (sender == NUD_Max)
            {
                MaxAngle = (int)NUD_Max.Value;
            }
        }

        private void TB_ID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '\b')
            {

            }
            else
            {
                e.KeyChar = '\0';
            }
        }


        private void LAB_Click(object sender, EventArgs e)
        {
            if (sender == LAB_Del)
            {
                _isDelete = true;
            }
            else if (sender == LAB_MoveP)
            {
                _isMoveP = true;
            }
            else if (sender == LAB_MoveN)
            {
                _isMoveN = true;
            }
        }

        private void TB_Function_TextChanged(object sender, EventArgs e)
        {
            Function=TB_Function.Text;
        }

        private void TB_ID_TextChanged(object sender, EventArgs e)
        {
            ID = int.Parse(TB_ID.Text);
        }

        public delegate void AngleChanged(int index, int angle);

        public event AngleChanged angleChanged;



    }

}
